import {h, toRef} from 'vue'
import type {StnDataTableColumns} from 'st-ui-vue3-navie'
import {genTableCol, genLeftFixedTableCol, genRightFixedTableCol} from 'st-ui-vue3-navie'
import {NaSwitchTag, TableEditDelBtnGroup} from '@components'
import {objCopyDeep} from 'st-common-core'
import {DictObjPropNameEnum} from '../enums/dict-obj-prop-name-enum.ts'

/**
 * 字典信息表格列配置
 */
export const DictTableCols: StnDataTableColumns = [
  genLeftFixedTableCol('ID', DictObjPropNameEnum.ID, 100, false, {draggable: false}),
  genLeftFixedTableCol('字典名称', DictObjPropNameEnum.NAME, 150, true),
  genTableCol('字典编码', DictObjPropNameEnum.CODE, 200, true),
  genTableCol('是否禁用', DictObjPropNameEnum.DISABLED, 100, false, {
    render: (row) => h(
      NaSwitchTag,
      {
        value: !row[DictObjPropNameEnum.DISABLED],
        onText: '启用',
        offText: '禁用'
      }
    )
  }),
  genTableCol('排序', DictObjPropNameEnum.SORT, 100, false, {sorter: (a, b) => a.sort - b.sort}),
  genTableCol('备注', DictObjPropNameEnum.REMARK, 800, true, {align: 'left'}),
  genRightFixedTableCol('操作', 'actions', 100, false, {
    draggable: false,
    render: row => h(
      TableEditDelBtnGroup,
      {
        form: toRef(objCopyDeep({
          origin: row,
        })).value,
        'update:form': (newForm) => {
          for (const key in newForm) {
            Reflect.set(row, key, Reflect.get(newForm, key))
          }
        },
        getKey: () => row.id,
      }
    )
  }),
] 